package com.demo.server;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import com.demo.data.Data;

public class ServerGui extends JFrame implements ActionListener, Runnable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField taPort;
	private JTextArea taLog;
	private JLabel labelPort;
	private JLabel labelAuthor;
	private JButton btnStart;
	private JButton btnStop;
	
	private JPanel jpNorth;
	private JPanel jpCenter;
	private JPanel jpSouth;
	
	private Dimension screenSize;
	private BorderLayout borderLayout;
	private GridBagLayout gbl_panelNorth;
	
	private Server mServer;
	
	public ServerGui() 
	{
		init();
	}
	
	public void init() 
	{
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int desktopWidth = (int) screenSize.getWidth();
		int desktopHeight = (int) screenSize.getHeight();
		int width = 500;
		int height = 300;
		
		jpNorth = new JPanel();
		getContentPane().add(jpNorth, BorderLayout.NORTH);
		gbl_panelNorth = new GridBagLayout();
		gbl_panelNorth.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_panelNorth.rowHeights = new int[] { 0, 0 };
		gbl_panelNorth.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0,0.0, Double.MIN_VALUE };
		gbl_panelNorth.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		jpNorth.setLayout(gbl_panelNorth);

		JLabel lblMarginLeft = new JLabel("");
		GridBagConstraints gbc_lblMarginLeft = new GridBagConstraints();
		gbc_lblMarginLeft.insets = new Insets(0, 0, 0, 5);
		gbc_lblMarginLeft.gridx = 0;
		gbc_lblMarginLeft.gridy = 0;
		jpNorth.add(lblMarginLeft, gbc_lblMarginLeft);

		labelPort = new JLabel("Port");
		GridBagConstraints gbc_lblPort = new GridBagConstraints();
		gbc_lblPort.anchor = GridBagConstraints.WEST;
		gbc_lblPort.insets = new Insets(5, 0, 0, 5);
		gbc_lblPort.gridx = 1;
		gbc_lblPort.gridy = 0;
		jpNorth.add(labelPort, gbc_lblPort);

		
		taPort = new JTextField();
		GridBagConstraints gbc_tfPort = new GridBagConstraints();
		gbc_tfPort.insets = new Insets(5, 0, 0, 5);
		gbc_tfPort.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPort.gridx = 2;
		gbc_tfPort.gridy = 0;
		jpNorth.add(taPort, gbc_tfPort);
		taPort.setColumns(10);

		btnStart = new JButton("Start");
		btnStart.addActionListener(this);
		GridBagConstraints gbc_btnOpen = new GridBagConstraints();
		gbc_btnOpen.insets = new Insets(5, 0, 0, 5);
		gbc_btnOpen.gridx = 3;
		gbc_btnOpen.gridy = 0;
		jpNorth.add(btnStart, gbc_btnOpen);
		// Nhấn nút mở thì ra sao
		btnStart.addActionListener(this);

		btnStop = new JButton("Stop");
		btnStop.setEnabled(false);
		btnStop.addActionListener(this);
		GridBagConstraints gbc_btnClose = new GridBagConstraints();
		gbc_btnClose.insets = new Insets(5, 0, 0, 5);
		gbc_btnClose.gridx = 4;
		gbc_btnClose.gridy = 0;
		jpNorth.add(btnStop, gbc_btnClose);
		// Nhấn nút đóng thì ra sao
		btnStop.addActionListener(this);

		JLabel lblMarginRight = new JLabel("");
		GridBagConstraints gbc_lblMarginRight = new GridBagConstraints();
		gbc_lblMarginRight.gridx = 5;
		gbc_lblMarginRight.gridy = 0;
		jpNorth.add(lblMarginRight, gbc_lblMarginRight);

		
		jpCenter = new JPanel();
		getContentPane().add(jpCenter, BorderLayout.CENTER);
		jpCenter.setLayout(new BorderLayout(0, 5));

		taLog = new JTextArea();
		jpCenter.add(taLog, BorderLayout.CENTER);

		jpSouth = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		labelAuthor = new JLabel("Duong Tuan Anh");
		jpSouth.add(labelAuthor);
		getContentPane().add(jpSouth, BorderLayout.SOUTH);

		setTitle("Server");
		borderLayout = (BorderLayout) getContentPane().getLayout();
		borderLayout.setVgap(3);		
		setBounds((desktopWidth - width) / 2, (desktopHeight - height) / 2, 500, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public void startServer() 
	{
		mServer = new Server();	
		new Thread(this).start();
		btnStart.setEnabled(false);
		btnStop.setEnabled(true);		
	}
	
	public void stopServer() 
	{
		mServer.stopServer();
		btnStart.setEnabled(true);
		btnStop.setEnabled(false);
	}
	
	public void openLogFile() 
	{
		
	}
	@Override
	public void actionPerformed(ActionEvent acEvent) 
	{
		switch (acEvent.getActionCommand()) 
		{
			case "Start":
				startServer();
				break;
			case "Stop":
				stopServer();
				break;
			case "Open":
				openLogFile();
				break;
			default:
				break;
		}
	}
	
	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() 
		{
			public void run() 
			{
				try 
				{
					for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) 
					{
						if ("Nimbus".equals(info.getName())) 
						{
							UIManager.setLookAndFeel(info.getClassName());
							break;
						}
					}
				} 
				catch (Exception e) 
				{
					try 
					{
						UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
					} 
					catch (Exception ex) 
					{
						
					}
				}
				try 
				{
					new ServerGui();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void run() 
	{
//		mServer.startServer();
	}
}
